import utils from '../utils'
function guessInputFileType(filename) {
  const ext = utils.getFileExtension(filename || '').toLowerCase()
  let type = null
  if (ext === 'dbf' || ext === 'shp' || ext === 'prj' || ext === 'shx') {
    type = ext
  } else if (/json$/.test(ext)) {
    type = 'json'
  } else if (ext === 'csv' || ext === 'tsv' || ext === 'txt' || ext === 'tab') {
    type = 'text'
  }
  return type
}
function guessInputContentType(content) {
  let type = ''
  if (utils.isString(content)) {
    type = stringLooksLikeJSON(content) ? 'json' : 'text'
  } else if (utils.isObject(content) && content.type || utils.isArray(content)) {
    type = 'json'
  }
  return type
}
function stringLooksLikeJSON(str) {
  return /^\s*[{[]/.test(String(str))
}
function guessInputType(filename, content) {
  return guessInputFileType(filename) || guessInputContentType(content)
}
function couldBeDsvFile(filename) {
  const ext = utils.getFileExtension(filename).toLowerCase()
  return /csv|tsv|txt$/.test(ext)
}
function isZipFile(filename) {
  return /\.zip$/i.test(filename)
}
function isSupportedBinaryInputType(path) {
  const ext = utils.getFileExtension(path).toLowerCase()
  return ext === 'shp' || ext === 'shx' || ext === 'dbf'
}
export default {
  isZipFile,
  guessInputFileType,
  couldBeDsvFile,
  isSupportedBinaryInputType,
  guessInputType,
}